<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use App\Models\Role;

/**
 * App\Models\User
 *
 * @property int $id
 * @property string|null $first_name
 * @property string|null $last_name
 * @property string $display_name
 * @property string|null $url
 * @property string|null $twitter
 * @property string|null $facebook
 * @property string|null $github
 * @property string|null $address
 * @property string|null $city
 * @property string|null $country
 * @property int $country_id
 * @property string|null $bio
 * @property string|null $job
 * @property string|null $phone
 * @property string|null $gender
 * @property string|null $relationship
 * @property string|null $birthday
 * @property string $email
 * @property string $password
 * @property string|null $remember_token
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Like[] $likes
 * @property-read \App\Models\Post $post
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Role[] $roles
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereBio($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereBirthday($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereCountry($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereCountryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereDisplayName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereFacebook($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereFirstName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereGender($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereGithub($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereJob($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereLastName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereRelationship($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereTwitter($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereUrl($value)
 * @mixin \Eloquent
 */
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['first_name', 'last_name', 'display_name', 'url', 'twitter', 'facebook', 'github', 'address', 'city', 'country', 'bio', 'job', 'phone', 'gender', 'relationship', 'birthday', 'email', 'password'];

    protected $casts = [
        'country_id' => 'boolean',
    ];

    protected $hidden = ['id', 'roles'];
    // protected $visible = ['first_name', 'last_name'];
    protected $appends = ['is_admin'];
    /**
     * Get the route key for the model.
     *
     * @return string
     */
    // public function getRouteKeyName()
    // {
    //     return 'id';
    // }

    /**
     * 和角色的多对多关系
     */
    public function roles() {
        return $this->belongsToMany('App\Models\Role');
    }

    /**
     * 获取所有用户点赞
     */
    public function likes() {
        return $this->morphMany('App\Models\Like', 'likeable');
    }

    /**
     * 和国家一对一的关联关系
     */
    public function post()
    {
        return $this->hasOne('App\Models\Post');
    }

    /**
     * 真正的一对一关系
     */
    // public function phone()
    // {
    //     return $this->hasOne('App\Models\Phone');
    // }

    /**
     * 一对一的关联与 phone
     */
    public function phone()
    {
        return $this->hasOne('App\Models\Phone');
    }

    public function testc()
    {
        return $this->hasOne('App\Models\Testc', 'uid');
    }

    /**
     * 定义一个访问器 first_name
     */
    public function getFirstNameAttribute($value)
    {
        return $value . '哈';
    }

    /**
     * 为 append 字段指定访问器
     */
    public function getIsAdminAttribute($value)
    {
        return $this->attributes['facebook'] == 'yes';
    }

    /**
     * 定义一个修改器 first_name
     */
    public function setFirstNameAttribute($value)
    {
        $this->attributes['first_name'] = $value . '嘿';
    }
}
